package com.shd.edanpei.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.bean.TeamModel;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.presenter.TeamPresenter;
import com.shd.edanpei.presenter.contract.TeamContract;
import com.shd.edanpei.util.DataCleanManager;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by mc on 2018/11/20.
 * 团队
 */

public class TeamActivity extends BaseActivity<TeamPresenter> implements TeamContract.TeamIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.ll_noTeam)
    View noTeamLayout;
    @BindView(R.id.ll_haveTeam)
    View haveTeamLayout;
    @BindView(R.id.et_teamCode)
    EditText etTeamCode;
    @BindView(R.id.tv_teamName)
    TextView tvTeamName;
    @BindView(R.id.tv_teamCode)
    TextView tvTeamCode;
    @BindView(R.id.iv_teamLogo)
    CircleImageView ivTeamLogo;
    @BindView(R.id.tv_exit)
    TextView tvExit;

    private TeamModel model;
    private LoadImageCtrl loadImageCtrl;

    @Override
    protected int setLayout() {
        return R.layout.activity_team;
    }

    @Override
    protected TeamPresenter bindPresenter() {
        return new TeamPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

        loadImageCtrl = new LoadImageCtrl(this);

        headline.setText("我的团队");

        mPresenter.queryTeamInfo();

    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }

    /**
     * 加入团队
     */
    @OnClick(R.id.tv_join)
    void onJoin() {
        String code = etTeamCode.getText().toString().trim();
        if (!TextUtils.isEmpty(code)) {
            mPresenter.joinTeam(code);
        }
    }


    /**
     * 进入团队
     */
    @OnClick(R.id.ll_haveTeam)
    void onTeam() {
        //判断角色
        //中介老板角色：进入团队管理
//        startActivity(MyTeamActivity.class);
        //其他非银行经理角色：进入业务员详情
//        Bundle bundle = new Bundle();
//        bundle.putInt(Constant.USER_ID, model.getUserId());
//        startActivity(SalesmanActivity.class, bundle);
    }

    /**
     * 退出团队
     */
    @OnClick(R.id.tv_exit)
    void onExitTeam() {

        if (model.getCurrentState() == 1) {
            String str = "您确定要退出<font color='" + getResources().getColor(R.color.main_color) + "'>" + model.getTeamName() + "</font>团队吗？";
            String noSubmit="<font color='" + getResources().getColor(R.color.main_color) + "'>" + "取消" + "</font>";
            String okSubmit="<font color='" + getResources().getColor(R.color.main_color) + "'>" + "确定" + "</font>";
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("退团确认")
                    .setMessage(Html.fromHtml(str))
                    .setNegativeButton(Html.fromHtml(noSubmit), null)
                    .setPositiveButton(Html.fromHtml(okSubmit), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mPresenter.exitTeam(2);
                            dialog.dismiss();
                        }
                    });
            AlertDialog alertDialog=builder.create();
            alertDialog.show();
        } else if (model.getCurrentState() == 2) {
//            tvExit.setText("立即撤销");
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("撤销申请")
                    .setMessage("您确定要撤销此申请吗？")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mPresenter.exitTeam(1);
                        }
                    })
                    .show();

        }


    }


    @Override
    public void haveTeam(TeamModel data) {
        model = data;
        noTeamLayout.setVisibility(View.GONE);
        haveTeamLayout.setVisibility(View.VISIBLE);
        tvTeamName.setText(data.getTeamName());
        tvTeamCode.setText(new StringBuffer().append("团队码：").append(data.getTeamCode()));
        if (!TextUtils.isEmpty(data.getTeamPic())) {
            loadImageCtrl.getGlide().load(data.getTeamPic()).into(ivTeamLogo);
        }
        if (data.getCurrentState() == 1) {
            tvExit.setText("退团");
        } else if (data.getCurrentState() == 2) {
            tvExit.setText("立即撤销");
        }
    }

    @Override
    public void noTeam() {
        haveTeamLayout.setVisibility(View.GONE);
        noTeamLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void clickExit() {
        model.setCurrentState(2);
        tvExit.setText("立即撤销");
    }

    @Override
    public void cancelExit() {
        model.setCurrentState(1);
        tvExit.setText("退团");
    }
}


